define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','form','layer'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template,Form,Layer) {

    var Controller = {
        index: function () {
            /**
             * 查询合作记录
             */
            function cooperation() {
                //读取FastAdmin的更新信息和社区动态
                $.ajax({
                    url: 'crm/cooperation/index',
                    type: 'post',
                    dataType: 'json',
                    success: function (ret) {
                        console.log(ret)
                        $("#news-list").html(Template("newstpl", {news: ret.rows}));
                    }
                });
            }
            cooperation();
            /**
             * 查询任务
             * @param type
             */
            function work_type(type) {
                $.ajax({
                    url: 'crm/adminwork/work_type',
                    type: 'post',
                    data:{type:type},
                    dataType: 'text',
                    success: function (ret) {
                        ret = JSON.parse(ret);
                        $("#discussion-list").html(Template("discussiontpl", {news: ret}));
                        Layer.closeAll('loading'); //关闭加载层
                    },error:function (e) {
                        alert('网络故障');
                    }
                });
            }
            work_type(1);
            /**
             * 时间段查询
             * */
            $('#work_type .work_list').click(function () {
                var link = $(this);
                Layer.load(2, {shade: [0.2, '#eeeeee']});
                var text = link.find('a').text();
                var data_type = link.attr('data-type');
                $('#dropdownMenu1').find('span').eq(0).text(text)
                work_type(data_type)
            })
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['人名币']
                },
                toolbox: {
                    show: false,
                    feature: {
                        magicType: {show: true, type: ['stack', 'tiled']},
                        saveAsImage: {show: true}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Orderdata.column
                },
                yAxis: {},
                grid: [{
                    left: 'left',
                    top: 'top',
                    right: '10',
                    bottom: 30
                }],
                series: [{
                    name: __('金额'),
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 1.5
                        }
                    },
                    data: Orderdata.paydata
                },
                   /* {
                        name: __('Orders'),
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: Orderdata.createdata
                    }*/
                    ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
            var time = '';
            function dingshi() {
                   time =   setInterval(function () {
                       if(day>=0) {
                           $.ajax({
                               url: 'dashboard/ajax_eachert',
                               dataType: 'text',
                               data: {day:day},
                               type: 'POST',
                               success: function (data) {
                                   data = JSON.parse(data);
                                   if(data['code']==1){
                                       //存在数据
                                       //动态添加数据，可以通过Ajax获取数据然后填充
                                       Orderdata.column.push(data['data'][0]['time']);
                                       /* Orderdata.createdata.push(amount);*/
                                       Orderdata.paydata.push(data['data'][0]['much']);
                                       //按自己需求可以取消这个限制
                                       if (Orderdata.column.length >= 20) {
                                           //移除最开始的一条数据
                                           Orderdata.column.shift();
                                           Orderdata.paydata.shift();
                                           /* Orderdata.createdata.shift();*/
                                       }
                                       myChart.setOption({
                                           xAxis: {
                                               data: Orderdata.column
                                           },
                                           series: [{
                                               name: __('金额'),
                                               data: Orderdata.paydata
                                           },
                                               /* {
                                                    name: __('Orders'),
                                                    data: Orderdata.createdata
                                                }*/]
                                       });
                                       if ($("#echart").width() != $("#echart canvas").width() && $("#echart canvas").width() < $("#echart").width()) {
                                           myChart.resize();
                                       }
                                   }
                               }, error: function (e) {
                                   Layer.msg('网络故障');
                               }
                           })
                           day--;
                       }else {
                           //停止定时器
                           clearInterval(time);
                       }
                   }, 2000);

            }
            dingshi();

            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });



            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});